package logIn;

import wareHouse.InHouse;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

/**
 * MySQL项目,仓库管理系统主界面
 * @Author Jian Chen long
 * @Date 2021/6/15 19:01
 */
public class MainInterFace extends JFrame {
    private JLabel label = new JLabel("仓库管理系统");
    private JPanel panel = new JPanel();
    private JPanel panel1 = new JPanel(new GridLayout(2,1,0,0));

    private JLabel logNumLabel = new JLabel("工号");
    private JTextField logText = new JTextField(18);

    private JLabel password = new JLabel("密码");
    // 密码输入框,看不到密码
    private JPasswordField passwordText = new JPasswordField();
//    private JTextField passwordText = new JTextField(18);

    private JPanel panel2 = new JPanel(new GridLayout(1,1,0,0));
    private JButton logButton = new JButton("登录");
//    private JButton newButton = new JButton("注册");

    public MainInterFace(){
        panel.add(label);
        panel1.add(logNumLabel);
        panel1.add(logText);
        panel1.add(password);
        panel1.add(passwordText);
        panel2.add(logButton);
//        panel2.add(newButton);
        panel.add(panel1,BorderLayout.NORTH);
        panel.add(panel2,BorderLayout.SOUTH);
        this.add(panel);

        this.setTitle("仓库管理系统");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setSize(450,180);
        this.setLocationRelativeTo(null);
        this.setVisible(true);
//        this.setResizable(true);

        /**
         * 登录按钮功能实现
         */
        logButton.addActionListener(new AbstractAction() {
            @Override
            public void actionPerformed(ActionEvent e) {
                // 得到工号、登陆密码
                String logNum = logText.getText();
                String password = passwordText.getText();

                Connection conn = null;
                PreparedStatement ps = null;
                ResultSet rs = null;

                try{
                    //没有填写完整的信息,如果没有填写完整,弹出消息框
                    if (logNum.length() == 0 || password.length() == 0){
                        JOptionPane.showMessageDialog(null,"登录名或密码填写不完整");
                        return;
                    }
                    //获取连接
                    conn = MysqlConnectionTool.getConnection();
                    // 查询从users表中查询userno
                    String sql = "select userno from users";
                    ps = conn.prepareStatement(sql);
                    rs = ps.executeQuery();
                    //判断是否有这个用户
                    while (rs.next()){
                        if (logNum.equals(rs.getString("userno")) == true) {
                            // 判断密码是否正确
                            String sql2 = "select userPassword from users where userno = " + logNum;
                            ps = conn.prepareStatement(sql2);
                            rs = ps.executeQuery();
                            // 判断密码是否正确
                            while (rs.next()){
                                if (password.equals(rs.getString("userPassword")) == false){
                                    JOptionPane.showMessageDialog(null,"密码错误,请重新输入");
                                    return;
                                }else {
                                    // 如果程序执行到这,说明账户密码正确
                                    // 判断是否为管理员登录
                                    String sql3 = "select 姓名 from userview where 编号 = " + logNum;
                                    ps =  conn.prepareStatement(sql3);
                                    rs = ps.executeQuery();
                                    while (rs.next()){
                                        if ("admin".equals(rs.getString("姓名")) == true){
                                            /**
                                             * 此处为管理员登陆成功,调用管理员界面
                                             */
                                            JOptionPane.showMessageDialog(null,"管理员登陆成功");
                                            System.out.println("管理员登陆成功");
                                            new Manage().setVisible(true);
                                        }else {
                                            /**
                                             * 此处为员工登陆成功,调用员工界面
                                             */
                                            System.out.println("员工登陆成功");
                                            JOptionPane.showMessageDialog(null,"员工登陆成功");
                                            new InHouse().setVisible(true);
                                        }
                                    }
                                    return;
                                }
                            }
                        }
                    }
                    // 如果程序执行到这,说明用户不存在,弹出提示信息:未注册,请先注册
                    JOptionPane.showMessageDialog(null,"未注册,请先注册");
                    return;

                }catch (Exception E){
                    E.printStackTrace();
                }finally {
                    MysqlConnectionTool.close(conn,ps,rs);
                }

            }
        });
    }


}
